/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Xml;
using System.IO;
using EXtremecode.DataAccessLayer;
using EXtremecode.EntityDefinition; 


namespace EXtremecode.EntityDefinitionGenerator
{
	/// <summary>
	/// Summary description for UserInterface.
	/// </summary>
	internal class UserInterface
	{
		
		internal static XmlDocument GetEntityDefinitionDocument()
		{
			string strPath=EntityDefinition.Configurations.Provider.EntityDefinitionPath;
			XmlDocument doc=new XmlDocument();
			if(File.Exists(strPath))
			{
				try
				{
					doc.Load(strPath);
				}
				catch{}
			}
			return doc;
		}
		internal static XmlDocument GetEntityCustomized()
		{
			string strPath=Configurations.Provider.EntityCustomizedPath;
			XmlDocument doc=new XmlDocument();
			if(File.Exists(strPath))
			{
				try
				{
					doc.Load(strPath);
				}
				catch{}
			}
			return doc;
		}
		internal static void WriteEntityDefinition(XmlElement parentElement)
		{
			WriteEntityDefinition(parentElement,string.Empty);
		}
		internal static void WriteEntityDefinition(XmlElement parentElement,string fileNamePrefix)
		{
			string strPath=EntityDefinition.Configurations.Provider.EntityDefinitionPath ;
			FileInfo fileInfo=new FileInfo(strPath);
			FileStream fileStream = new FileStream(
						   fileInfo.DirectoryName + "\\" + fileNamePrefix + fileInfo.Name ,FileMode.Create); 
			XmlTextWriter xmlTextWriter = new XmlTextWriter(fileStream,System.Text.Encoding.UTF8);
			xmlTextWriter.Formatting=Formatting.Indented; 
			parentElement.WriteTo(xmlTextWriter);
			xmlTextWriter.Flush();
			xmlTextWriter.Close();
		}
		internal static void CreateClasses()
		{
			string strEntityDefPath=EntityDefinition.Configurations.Provider.EntityDefinitionPath;
			string strClassDirPath=Configurations.Provider.ClassesDirectoryPath;
			string strStyleSheetPath=Configurations.Provider.StyleSheetPath;
			XmlDocument doc= GetEntityDefinitionDocument();
			Entity.ExtendXml(doc.DocumentElement); 
			DirectoryInfo dirClasses=null;
			if(!Directory.Exists(strClassDirPath))
				dirClasses=Directory.CreateDirectory(strClassDirPath);
			else
				dirClasses=new DirectoryInfo(strClassDirPath);
			BusinessObjectGenerator.GenerateCode(doc,strStyleSheetPath,Configurations.Provider.Namespace,dirClasses); 
		}
		internal static void CreateXtraCodeClass()
		{
			string strEntityDefPath=EntityDefinition.Configurations.Provider.EntityDefinitionPath;
			string strStyleSheetPath=Configurations.Provider.XtraCodeStyleSheetPath;
			XmlDocument doc= GetEntityDefinitionDocument();
			Entity.ExtendXml(doc.DocumentElement); 
			BusinessObjectGenerator.GenerateXtraCode(doc,strStyleSheetPath,Configurations.Provider.Namespace,Configurations.Provider.XtraCodePath,Configurations.Provider.XtraCodeClassName,Configurations.Provider.XtraCodeNamespace); 
			
		}
		private static DataTable GetDataTableForXmlFile(string filePath)
		{
			DataSet ds=new DataSet();
			ds.ReadXml(filePath);
			if(ds.Tables.Count>0)return ds.Tables[0];
			return null;

		}
		
	}
}
